package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.course.CourseDTO;
import com.qk.entity.Course;
import com.qk.management.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * @author sch
 * @version 1.0
 * @since 2025-10-09-10:40
 */
@Slf4j
@RestController
@RequestMapping("/courses")
public class CourseController {
    @Autowired
    private CourseService courseService;

    @GetMapping
    public Result listCourse(CourseDTO courseDTO) {
        PageResult<Course> pageResult = courseService.listCourse(courseDTO);
        log.info("查询课程列表: {}", pageResult);
        return Result.success(pageResult);
    }

    @PostMapping
    public Result addCourse(@RequestBody Course course) {
        log.info("添加课程", course);
        courseService.addCourse(course);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result deleteCourse(@PathVariable Integer id) {
        log.info("删除课程", id);
        courseService.deleteCourse(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getCourseById(@PathVariable Integer id) {
        log.info("查询课程", id);
        Course course = courseService.getCourseById(id);
        return Result.success(course);
    }

    @PutMapping
    public Result updateCourse(@RequestBody Course course) {
        log.info("更新课程", course);
        courseService.updateCourse(course);
        return Result.success();
    }

    @GetMapping("/list")
    public Result findAllCourses() {
        log.info("查询课程列表");
        List<Course> list = courseService.findAllCourses();
        return Result.success(list);
    }

    @GetMapping("/subject/{subject}")
    public Result listCoursesBySubject(@PathVariable Integer subject) {
        log.info("根据学科查询课程: subject={}", subject);
        List<Course> courses = courseService.listCoursesBySubject(subject);
        return Result.success(courses);
    }
}
